package tasks;

import system.*;

import Main.Parameters;
import java.io.File;
import java.io.FilenameFilter;


/**
 * Find | Move | Rename Tasks
 * @author zem
 */
public class FMR {

    /**
     * Move task file to specified location
     */
    public void Move() {
        String FileTask=""+Parameters.TaskFolder+Parameters.TaskName[Parameters.CurrentTaskNumber];
        if(!Parameters.TaskFailed){ //task done
        File path = new File(new PathName(Parameters.MoveDoneTaskTo, 1).name); //folder where to put files
        if (!path.exists()) {
            path.mkdirs(); //create folder with all parent folders
        }
        if (path.exists()) { //if folder exists
            File pth=new File(new PathName(Parameters.MoveDoneTaskTo, 1).name + Parameters.TaskName[Parameters.CurrentTaskNumber]);
            if(pth.exists()){pth.delete();}
            new File(FileTask).renameTo(pth);
        } else { //in else case error printing and add extension
             String[] mes = {Parameters.MoveDoneTaskTo,Parameters.TaskName[Parameters.CurrentTaskNumber]};
            new MessageOUTPUT().OUTPUT("er0002",mes);
           Rename(); //rename

        }
    }else
    {
     File path = new File(new PathName(Parameters.MoveFailedTaskTo, 1).name); //folder where to put files
        if (!path.exists()) {
            path.mkdirs(); //create folder with all parent folders
        }
        if (path.exists()) { //if folder exists
            new File(FileTask).renameTo(new File(new PathName(Parameters.MoveFailedTaskTo, 1).name + Parameters.TaskName[Parameters.CurrentTaskNumber]));
        } else { //in else case error printing and add extension
             String[] mes = {Parameters.MoveFailedTaskTo, Parameters.TaskName[Parameters.CurrentTaskNumber]};
            new MessageOUTPUT().OUTPUT("er0002",mes);
            Rename(); //rename

        }



    }
}

    /**
     * Rename file task - adding some extension
     */
    public void Rename(){
          String FileTask=""+Parameters.TaskFolder+Parameters.TaskName[Parameters.CurrentTaskNumber];
          if(!Parameters.TaskFailed){ //task done

                boolean rename = new File(FileTask).renameTo(new File(FileTask + Parameters.MoveDoneTaskExtension)); //renaming
            if (rename) {//if succesfull
                  String[] mes = {Parameters.TaskName[Parameters.CurrentTaskNumber],Parameters.TaskName[Parameters.CurrentTaskNumber]+Parameters.MoveDoneTaskExtension};
                 new MessageOUTPUT().OUTPUT("ev0004",mes);
            } else {//if not
                  String[] mes = {Parameters.TaskName[Parameters.CurrentTaskNumber],Parameters.TaskName[Parameters.CurrentTaskNumber]+Parameters.MoveDoneTaskExtension};
                new MessageOUTPUT().OUTPUT("er0003",mes);
                }

            }else{
               boolean rename = new File(FileTask).renameTo(new File(FileTask + Parameters.MoveFailedTaskExtension)); //renaming
            if (rename) {//if succesfull
                  String[] mes = {Parameters.TaskName[Parameters.CurrentTaskNumber], Parameters.TaskName[Parameters.CurrentTaskNumber]+Parameters.MoveFailedTaskExtension};
                 new MessageOUTPUT().OUTPUT("ev0004",mes);
            } else {//if not
                  String[] mes = {Parameters.TaskName[Parameters.CurrentTaskNumber],Parameters.TaskName[Parameters.CurrentTaskNumber]+Parameters.MoveFailedTaskExtension};
                 new MessageOUTPUT().OUTPUT("er0003",mes);
                }
            }


        }

     /**
      * Find tasks
      */
     public void Find(){

        //getting files with tasks
        Parameters.TotalTasks=0;
        File f1 = new File(Parameters.TaskFolder);
        int i=0;
        String[] ls;
        FilenameFilter filter = new TaskFilter();
        ls = f1.list(filter);

        for (i = 0; ls != null && i < ls.length;  i++) {
            //checking if file is not already done:
            boolean task_done=false; //new task
            if(Parameters.DeleteTaskWhenDone==0){
            if(Parameters.MoveDoneTask==0 |Parameters.MoveFailedTask==0){
                for(int j=0; j<Parameters.TasksDone; j++){
                        if(ls[i].equals(Parameters.AdrOfDoneTasks[j])){task_done=true; j=Parameters.TasksDone;}
                        }

            }
            }

            if(!task_done){ //if new task

            //adding task
            Parameters.TaskName[Parameters.TotalTasks] = ls[i];
            Parameters.taskAssignTime[Parameters.TotalTasks]= Parameters.CurrentSystemTime;
            Parameters.TotalTasks++;


            }

        }



    }
}
/**
 * Filter for Task Files
 * @author ZeM
 */
class TaskFilter implements FilenameFilter { //filter

    public boolean accept(File dir, String name) {
        return (name.endsWith(Parameters.FileTaskExtension));
    }
}
